package io.renren.modules.region.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.region.entity.TResidentEntity;
import io.renren.modules.region.entity.vo.ResidentVO;
import io.renren.modules.region.service.TResidentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 居民数据接口
 *
 * @author lhb
 * @date 2023/5/26
 */

@RestController
@RequestMapping("resident")
public class ResidentController {

    @Autowired
    private TResidentService residentService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("resident:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = residentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{code}")
    public R info(@PathVariable("code") String code) {
        ResidentVO residentVO = residentService.queryResidentInfo(code);

        return R.ok().put("resident", residentVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("resident:save")
    public R save(@RequestBody TResidentEntity resident) {

        try {
            residentService.save(resident);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("resident:update")
    public R update(@RequestBody TResidentEntity resident) {
        residentService.updateById(resident);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("resident:delete")
    public R delete(@RequestBody String[] codes) {
        residentService.deleteResident(Arrays.asList(codes));
        return R.ok();
    }
}
